﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace DbSetupCreator
{
    public partial class FormLogOn : Form
    {
        private SqlConnection connection;
        private StringBuilder connectionString = new StringBuilder();

        public FormLogOn()
        {
            InitializeComponent();
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            try
            {
                //create needed objects.
                connection = new SqlConnection();
                connectionString.Remove(0, connectionString.Length); // clear the string.

                //build the connection string
                connectionString.Append("Data Source=");
                connectionString.Append(tbServer.Text);
                connectionString.Append(";Initial Catalog=master;User Id=");
                connectionString.Append(tbUserName.Text);
                connectionString.Append(";Password=");
                connectionString.Append(tbPassword.Text);
                connectionString.Append(";");

                connection.ConnectionString = connectionString.ToString();

                connection.Open();
                connection.Close();
                this.DialogResult = DialogResult.OK;
            }
            catch (SqlException)
            {
                MessageBox.Show("Your log on failed. Please try again.", "Failed log on.", 
                    MessageBoxButtons.OK);
            }
        }

        public string ConnectionString
        {
            get { return connectionString.ToString(); }
        }

        public SqlConnection Connection
        {
            get { return connection; } 
        }
    }
}
